package com.v1.video.option.util;

import java.util.List;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.iss.httpclient.NormalHttpClient;
import com.iss.httpclient.core.HttpRequestException;
import com.iss.httpclient.core.HttpResponse;
import com.iss.httpclient.core.ParameterList;
import com.iss.httpclient.core.ParameterList.StringParameter;
import com.v1.video.Constant;
import com.v1.video.domain.GetSinaFriendsPageInfo;
import com.v1.video.domain.HotAndMyThemePageInfo;
import com.v1.video.domain.HotThemePageInfo;
import com.v1.video.domain.LoginInfo;
import com.v1.video.domain.RecommendAppPageInfo;
import com.v1.video.domain.SearchAlbumPageInfo;
import com.v1.video.domain.SearchData2PageInfo;
import com.v1.video.domain.SearchGroupPageInfo;
import com.v1.video.domain.SearchPaikeInfo;
import com.v1.video.domain.SearchPaikePageInfo;
import com.v1.video.domain.SearchThemePageInfo;
import com.v1.video.domain.SearchUpdataFocousInfo;
import com.v1.video.domain.SearchUser2PageInfo;
import com.v1.video.domain.SearchVideo2PageInfo;
import com.v1.video.domain.SearchVideoInfo;
import com.v1.video.domain.SearchVideoPageInfo;
import com.v1.video.domain.SettingMessagePageInfo3;
import com.v1.video.domain.SettingMessageStatusPageInfo3;
import com.v1.video.exception.NetException;
import com.v1.video.exception.ServicesException;
import com.v1.video.util.Logger;

public class SearchAboutUtil {

    public final String TAG = "SearchAboutUtil";
    
    /**
     * 设置-应用推荐-列表
     * @return 
     * @throws ServicesException
     */
    public RecommendAppPageInfo getRecommendApp() throws Exception/*ServicesException*/ {
        ParameterList params = new ParameterList();
        params.add(new StringParameter("appType","android"));
        String json = null;
        try {
            json = getJsonByPost(Constant.OPTION_RECOMMEND_APP_LIST, params);
//            Logger.i(TAG, "设置应用推荐.json="+json);
        } catch (NetException e) {
//            Logger.i(TAG, "应用推荐这块报的异常");
            throw new ServicesException(e.getMessage());
        }
        if(json != null){
            Gson gson = new Gson();
            RecommendAppPageInfo detailsInfo = null;
            try {
                 detailsInfo = gson.fromJson(json, RecommendAppPageInfo.class);
                 if("1".equals(detailsInfo.getResult().getCode())){
                     throw new ServicesException(detailsInfo.getResult().getMessage());
                 }else{
                     return detailsInfo;
                 }
            } catch (JsonSyntaxException e) {
                throw new ServicesException("服务器返回数据错误");
            }   catch (Exception e) {
//                throw new ServicesException("服务器返回数据错误");
                e.printStackTrace();
            }
//            if("1".equals(detailsInfo.getResult().getCode())){
//                throw new ServicesException(detailsInfo.getResult().getMessage());                
//            }else{
//                return detailsInfo;
//            }
        }else{
            return null;
        }
        return null;
    }
    
    /***
     * 3期的消息接口；
     * @param userId
     * @param isPage
     * @param page
     * @param rows
     * @return
     * @throws ServicesException
     */
    public SettingMessagePageInfo3 getSettingMessage3(String userId,String isPage,String page,String rows) throws ServicesException {
        ParameterList params = new ParameterList();
        params.add(new StringParameter("userId",userId));
//        params.add(new StringParameter("userId","32064"));//
        params.add(new StringParameter("isPage","1"));
        params.add(new StringParameter("page",page));
        params.add(new StringParameter("rows",rows));
        String json = null;
        try {
            json = getJsonByPost(Constant.GET_SETTING_MESSAGE_ALL_3, params);
//            Logger.i(TAG, "评论/关注信息.json="+json);
        } catch (NetException e) {
            throw new ServicesException(e.getMessage());
        }
        if(json != null){
            Gson gson = new Gson();
            SettingMessagePageInfo3 detailsInfo = null;
            try {
                 detailsInfo = gson.fromJson(json, SettingMessagePageInfo3.class);
                 if (detailsInfo != null) {
 					return detailsInfo;
 				}
 				return null;
            } catch (JsonSyntaxException e) {
                throw new ServicesException("服务器返回数据错误");
            }
            
        }else{
            return null;
        }
    }
    
    /**用以更新消息的状态*/
    public SettingMessageStatusPageInfo3 setSettingMessageStatus(String status,String messageId) throws ServicesException {
        ParameterList params = new ParameterList();
        params.add(new StringParameter("id",messageId));
        params.add(new StringParameter("status",status));
        String json = null;
        try {
            json = getJsonByPost(Constant.GET_SETTING_MESSAGE_UPDATA, params);
//            Logger.i(TAG, "评论/关注信息.json="+json);
        } catch (NetException e) {
            throw new ServicesException(e.getMessage());
        }
        if(json != null){
            Gson gson = new Gson();
            SettingMessageStatusPageInfo3 detailsInfo = null;
            try {
                 detailsInfo = gson.fromJson(json, SettingMessageStatusPageInfo3.class);
                 if (detailsInfo != null) {
 					return detailsInfo;
 				}
 				return null;
            } catch (JsonSyntaxException e) {
                throw new ServicesException("服务器返回数据错误");
            }
            
        }else{
            return null;
        }
    }
    
    /**
     * 搜索列表中的拍客信息
     * @param videoId   视频id
     * @return 
     * @throws ServicesException
     */
    public SearchPaikePageInfo getSearchPaikePageInfolist(String pageIndex,String strKey,String userId) throws ServicesException {
        ParameterList params = new ParameterList();
//        if(!userId.equals(""))
        params.add(new StringParameter("userId",userId));
        params.add(new StringParameter("title",strKey));
        params.add(new StringParameter("page_index",pageIndex));
        String json = null;
        try {
//            Logger.i(TAG, "搜索拍客第几页=="+pageIndex);
            json = getJsonByPost(Constant.SEARCH_PAIKE_LIST, params);
//            Logger.i(TAG, "SearchPaikePageInfo.json="+json);
        } catch (NetException e) {
            throw new ServicesException(e.getMessage());
        }
        if(json != null){
            Gson gson = new Gson();
            SearchPaikePageInfo detailsInfo = null;
            try {
                 detailsInfo = gson.fromJson(json, SearchPaikePageInfo.class);
                 if("1".equals(detailsInfo.getResult().getCode())){
                     throw new ServicesException(detailsInfo.getResult().getMessage());
                 }else{
//                     SearchPageSize pageInfo = detailsInfo.getPage_size();
//                     Logger.i(TAG, "pageInfo.pagesize="+detailsInfo.getPage_size());
                     List<SearchPaikeInfo> listPaiKeInfo = detailsInfo.getVideo_list();
//                     Logger.i(TAG, "listPaiKeInfo.size="+listPaiKeInfo.size());
                     for(int i = 0; i < listPaiKeInfo.size();i++){
                         SearchPaikeInfo paikeInfo = listPaiKeInfo.get(i);
//                         Logger.i(TAG, "paikeInfo.getFocusUserName="+paikeInfo.getFocusUserName());
                     }
                 }
            } catch (JsonSyntaxException e) {
                throw new ServicesException("服务器返回数据错误");
            }
            if("1".equals(detailsInfo.getResult().getCode())){
                throw new ServicesException(detailsInfo.getResult().getMessage());
            }else{
                return detailsInfo;
            }
        }else{
            return null;
        }
    }
    
    /**
     * 搜索列表中的视频信息
     * @param videoId   视频id
     * @return 
     * @throws ServicesException
     */
    public SearchVideoPageInfo getSearchVideoPageInfolist(String pageIndex,String strKey,String userId) throws ServicesException {
        ParameterList params = new ParameterList();
//        params.add(new StringParameter("userId",userId));
//        params.add(new StringParameter("title",strKey));
        params.add(new StringParameter("keyword",strKey));
        params.add(new StringParameter("pageSize","20"));//一页多少条
        params.add(new StringParameter("page_index",pageIndex));
        String json = null;
        try {
//            Logger.i(TAG, "当前搜索视频第几页=="+pageIndex);
            json = getJsonByPost(Constant.SEARCH_VIDEO_LIST, params);
            Logger.i(TAG, "搜索视频.json="+json);
        } catch (NetException e) {
            throw new ServicesException(e.getMessage());
        }
        if(json != null){
            Gson gson = new Gson();
            SearchVideoPageInfo detailsInfo = null;
            try {
                 detailsInfo = gson.fromJson(json, SearchVideoPageInfo.class);
                 if("1".equals(detailsInfo.getResult().getCode())){
                     throw new ServicesException(detailsInfo.getResult().getMessage());
                 }else{
//                     SearchPageSize pageInfo = detailsInfo.getPage_size();
//                     Logger.i(TAG, "pageInfo.pageindex="+detailsInfo.getPage_size());
                     List<SearchVideoInfo> listVideoInfo = detailsInfo.getVideo_list();
//                     Logger.i(TAG, "listVideoInfo.size="+listVideoInfo.size());
                     for(int i = 0; i < listVideoInfo.size();i++){
                         SearchVideoInfo videoInfo = listVideoInfo.get(i);
//                         Logger.i(TAG, "videoInfo.getUserId="+videoInfo.getUserId());
                     }
                 }
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
                throw new ServicesException("服务器返回数据错误");
            }
            if("1".equals(detailsInfo.getResult().getCode())){
                throw new ServicesException(detailsInfo.getResult().getMessage());
            }else{
                return detailsInfo;
            }
        }else{
            return null;
        }
    }
    
    /**
     * 更新关注信息
     * @param videoId   视频id
     * @return 
     * @throws ServicesException
     */
    public int updataAttentionInfo(String userId,String focusUserId,String typeId) throws ServicesException {
        ParameterList params = new ParameterList();
        params.add(new StringParameter("typeId",typeId));
        params.add(new StringParameter("userId",userId));
        params.add(new StringParameter("focusUserId",focusUserId));
        String json = null;
        try {
            json = getJsonByPost(Constant.ADD_OR_CANCEL_ATTENTION, params);
//            Logger.i(TAG, "更新关注.json="+json);
        } catch (NetException e) {
            throw new ServicesException(e.getMessage());
        }
        SearchUpdataFocousInfo info = null;
        if(json != null){
            Gson gson = new Gson();                        
            try {                
                info = gson.fromJson(json, SearchUpdataFocousInfo.class);                
            } catch (JsonSyntaxException e) {
                
                throw new ServicesException("服务器返回数据错误");
            }
            
            return Integer.parseInt(info.getResult().getCode());
            
        }else{
            return -1;
        }
    }
    
    /**
     * 更新关注信息
     * @param videoId   视频id
     * @return 
     * @throws ServicesException
     */
    public SearchUpdataFocousInfo updataAttentionInfo1(String userId,String focusUserId,String typeId) throws ServicesException {
        ParameterList params = new ParameterList();
        params.add(new StringParameter("typeId",typeId));
        params.add(new StringParameter("userId",userId));
        params.add(new StringParameter("focusUserId",focusUserId));
        String json = null;
        try {
            json = getJsonByPost(Constant.ADD_OR_CANCEL_ATTENTION, params);
//            Logger.i(TAG, "更新关注.json="+json);
        } catch (NetException e) {
            throw new ServicesException(e.getMessage());
        }
        SearchUpdataFocousInfo info = null;
        if(json != null){
            Gson gson = new Gson();                        
            try {                
                info = gson.fromJson(json, SearchUpdataFocousInfo.class);                
            } catch (JsonSyntaxException e) {
                
                throw new ServicesException("服务器返回数据错误");
            }
            if(info != null){
            	return info;
            }else{
            	return null;
            }
//            return Integer.parseInt(info.getResult().getCode());
            
        }else{
            return null;
        }
    }
    
    /**
     * 搜索用户
     * @param keyword:关键词
     * @param size：每页个数
     * @param pageNo：当前第几页
     * @return
     * @throws ServicesException
     */
    public SearchUser2PageInfo getUserBySearch(String keyword,String size,String pageNo) throws ServicesException {
		ParameterList params = new ParameterList();
		params.add(new StringParameter("keyword", keyword));
		params.add(new StringParameter("pageSize", size));
		params.add(new StringParameter("pageNo", pageNo));
		if(LoginInfo.getInstance().isLogin()){
			params.add(new StringParameter("userId", LoginInfo.getInstance().getUserId()));
		}
		String json = null;
		try {
			json = getJsonByPost(Constant.GET_USER_BY_SEARCH, params);
		} catch (NetException e) {
			throw new ServicesException(e.getMessage());
		}
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			try {
				SearchUser2PageInfo userPageInfo = gson.fromJson(json,
						SearchUser2PageInfo.class);
				if (userPageInfo != null) {
					return userPageInfo;
				}
				return null;
			} catch (JsonSyntaxException e) {
				throw new ServicesException("服务器返回数据错误");
			}

		} else {
			return null;
		}
	}
    
    /**
     * 搜索圈子
     * @param keyword:关键词
     * @param size：每页个数
     * @param pageNo：当前第几页
     * @return
     * @throws ServicesException
     */
    public SearchGroupPageInfo getGroupBySearch(String keyword,String size,String pageNo) throws ServicesException {
		ParameterList params = new ParameterList();
		params.add(new StringParameter("keyword", keyword));
		params.add(new StringParameter("pageSize", size));
		params.add(new StringParameter("pageNo", pageNo));
		String json = null;
		try {
			json = getJsonByPost(Constant.GET_GROUP_BY_SEARCH, params);
		} catch (NetException e) {
			throw new ServicesException(e.getMessage());
		}
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			try {
				SearchGroupPageInfo groupPageInfo = gson.fromJson(json,
						SearchGroupPageInfo.class);
				if (groupPageInfo != null) {
					return groupPageInfo;
				}
				return null;
			} catch (JsonSyntaxException e) {
				throw new ServicesException("服务器返回数据错误");
			}

		} else {
			return null;
		}
	}
    
    /**
     * 获取话题 3期的
     * @param keyword
     * @param size
     * @param pageNo
     * @return
     * @throws ServicesException
     */
    public SearchThemePageInfo getThemeBySearch(String keyword,String size,String pageNo) throws ServicesException {
		ParameterList params = new ParameterList();
		params.add(new StringParameter("keyword", keyword));
		params.add(new StringParameter("pageSize", size));
		params.add(new StringParameter("pageNo", pageNo));
		String json = null;
		try {
			json = getJsonByPost(Constant.GET_THEME_BY_SEARCH, params);
		} catch (NetException e) {
			throw new ServicesException(e.getMessage());
		}
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			try {
				SearchThemePageInfo themePageInfo = gson.fromJson(json,
						SearchThemePageInfo.class);
				if (themePageInfo != null) {
					return themePageInfo;
				}
				return null;
			} catch (JsonSyntaxException e) {
				throw new ServicesException("服务器返回数据错误");
			}

		} else {
			return null;
		}
	}
    
    /**
     * 发布视频页进入的 搜索话题用，
     * @param name
     * @return
     * @throws ServicesException
     */
    public HotThemePageInfo getThemeBySearchWithFabu(String name) throws ServicesException {
		ParameterList params = new ParameterList();
		params.add(new StringParameter("keyword", name));
		String json = null;
		try {
			json = getJsonByPost(Constant.GET_HOT_THEME_BY_SEARCH, params);
		} catch (NetException e) {
			throw new ServicesException(e.getMessage());
		}
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			try {
				HotThemePageInfo themePageInfo = gson.fromJson(json,
						HotThemePageInfo.class);
				if (themePageInfo != null) {
					return themePageInfo;
				}
				return null;
			} catch (JsonSyntaxException e) {
				throw new ServicesException("服务器返回数据错误");
			}

		} else {
			return null;
		}
	}
    
    /***
     * 发布视频页进入的获取我的和热门的话题
     * @param userId
     * @return
     * @throws ServicesException
     */
    public HotAndMyThemePageInfo getHotThemeAndMyByFabu(String userId) throws ServicesException {
		ParameterList params = new ParameterList();
		params.add(new StringParameter("userId", userId));
		String json = null;
		try {
			json = getJsonByPost(Constant.GET_HOT_THEME_AND_MY, params);
			
		} catch (NetException e) {
			throw new ServicesException(e.getMessage());
		}
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			try {
				HotAndMyThemePageInfo themePageInfo = gson.fromJson(json,
						HotAndMyThemePageInfo.class);
				if (themePageInfo != null) {
					return themePageInfo;
				}
				return null;
			} catch (JsonSyntaxException e) {
				throw new ServicesException("服务器返回数据错误");
			}

		} else {
			return null;
		}
	}
    
    /**
     * 获取新浪微博好友列表
     * @param userId
     * @return
     * @throws ServicesException
     */
    public GetSinaFriendsPageInfo getSinaFriends(String userId) throws ServicesException {
		ParameterList params = new ParameterList();
		params.add(new StringParameter("userId", userId));
//		params.add(new StringParameter("userId", "35425"));
		String json = null;
		try {
			json = getJsonByPost(Constant.GET_SINA_FRIENDS, params);
		} catch (NetException e) {
			throw new ServicesException(e.getMessage());
		}
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			try {
				GetSinaFriendsPageInfo sinaPageInfo = gson.fromJson(json,
						GetSinaFriendsPageInfo.class);
				if (sinaPageInfo != null) {
					return sinaPageInfo;
				}
				return null;
			} catch (JsonSyntaxException e) {
				throw new ServicesException("服务器返回数据错误");
			}

		} else {
			return null;
		}
	}
    
    /**
     * 搜索专辑
     * @param keyword:关键词
     * @param size：每页个数
     * @param pageNo：当前第几页
     * @return
     * @throws ServicesException
     */
    public SearchAlbumPageInfo getAlbumBySearch(String keyword,String size,String pageNo) throws ServicesException {
		ParameterList params = new ParameterList();
		params.add(new StringParameter("keyword", keyword));
		params.add(new StringParameter("pageSize", size));
		params.add(new StringParameter("pageNo", pageNo));
		String json = null;
		try {
			json = getJsonByPost(Constant.GET_ALBUM_BY_SEARCH, params);
		} catch (NetException e) {
			throw new ServicesException(e.getMessage());
		}
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			try {
				SearchAlbumPageInfo albumPageInfo = gson.fromJson(json,
						SearchAlbumPageInfo.class);
				if (albumPageInfo != null) {
					return albumPageInfo;
				}
				return null;
			} catch (JsonSyntaxException e) {
				throw new ServicesException("服务器返回数据错误");
			}

		} else {
			return null;
		}
	}
    
    /**
     * 搜索总页	
     * @param keyword
     * @param size
     * @param pageNo
     * @return
     * @throws ServicesException
     */
    public SearchData2PageInfo getDataBySearch(String keyword,String size,String pageNo) throws ServicesException {
		ParameterList params = new ParameterList();
		params.add(new StringParameter("keyword", keyword));
		params.add(new StringParameter("pageSize", size));
		params.add(new StringParameter("pageNo", pageNo));
		String json = null;
		try {
			json = getJsonByPost(Constant.GET_ALL_BY_SEARCH, params);
		} catch (NetException e) {
			throw new ServicesException(e.getMessage());
		}
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			try {
				SearchData2PageInfo searchPageInfo = gson.fromJson(json,
						SearchData2PageInfo.class);
				if (searchPageInfo != null) {
					return searchPageInfo;
				}
				return null;
			} catch (JsonSyntaxException e) {
				e.printStackTrace();
				throw new ServicesException("服务器返回数据错误");
			}

		} else {
			return null;
		}
	}
    
    /**
     * 搜索视频第二页及以后的数据
     * @param keyword
     * @param size
     * @param pageNo
     * @return
     * @throws ServicesException
     */
    public SearchVideo2PageInfo getVideoBySearch(String keyword,String size,String pageNo) throws ServicesException {
		ParameterList params = new ParameterList();
		params.add(new StringParameter("keyword", keyword));
		params.add(new StringParameter("pageSize", size));
		params.add(new StringParameter("pageNo", pageNo));
		String json = null;
		try {
			json = getJsonByPost(Constant.GET_VIDEO_BY_SEARCH, params);
		} catch (NetException e) {
			throw new ServicesException(e.getMessage());
		}
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			try {
				SearchVideo2PageInfo videoPageInfo = gson.fromJson(json,
						SearchVideo2PageInfo.class);
				if (videoPageInfo != null) {
					return videoPageInfo;
				}
				return null;
			} catch (JsonSyntaxException e) {
				throw new ServicesException("服务器返回数据错误");
			}

		} else {
			return null;
		}
	}
    
    /**
     * 搜索视频第二页及以后的数据，搜索无数据，这是调的推荐
     * @param keyword
     * @param size
     * @param pageNo
     * @return
     * @throws ServicesException
     */
    public SearchVideo2PageInfo getVideoBySearchRecommend(String keyword,String size,String pageNo) throws ServicesException {
		ParameterList params = new ParameterList();
		params.add(new StringParameter("keyword", "0"));
		params.add(new StringParameter("pageSize", size));
		params.add(new StringParameter("pageNo", pageNo));
		String json = null;
		try {
			json = getJsonByPost(Constant.GET_VIDEO_BY_SEARCH_RECOMMEND, params);
		} catch (NetException e) {
			throw new ServicesException(e.getMessage());
		}
		if (!TextUtils.isEmpty(json)) {
			Gson gson = new Gson();
			try {
				SearchVideo2PageInfo videoPageInfo = gson.fromJson(json,
						SearchVideo2PageInfo.class);
				if (videoPageInfo != null) {
					return videoPageInfo;
				}
				return null;
			} catch (JsonSyntaxException e) {
				throw new ServicesException("服务器返回数据错误");
			}

		} else {
			return null;
		}
	}
    
   
    
    /**
     * 发送post请求，获取返回结果json字符串
     * @param path
     * @param params
     * @return
     * @throws NetException
     */
    private String getJsonByPost(String path, ParameterList params) throws NetException{
        NormalHttpClient mclient = new NormalHttpClient();
        HttpResponse response;
        try {
            response = mclient.post(path, params);
        } catch (HttpRequestException e) {
            e.printStackTrace();
            throw new NetException("网络请求异常，请检查网络");
        }
        String s = response.getBodyAsString();
        Logger.d("json", "json=" + s);
        return s;
    }
    
    /**
     * 发送get请求，获取返回结果json字符串
     * @param path
     * @param params
     * @return
     * @throws NetException
     */
    private String getJsonByGet(String path, ParameterList params) throws NetException{
        NormalHttpClient mclient = new NormalHttpClient();
        HttpResponse response;
        try {
            response = mclient.get(path, params);
        } catch (HttpRequestException e) {
            e.printStackTrace();
            throw new NetException("网络请求异常，请检查网络");
        }
        String s = response.getBodyAsString();
        Logger.d("json", "json=" + s);
        return s;
    }
}
